package com.sum.slike;

import ohos.agp.render.Paint;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zhu_zhonglin
 * @time: 2021/4/26
 */
public class TextAnimationFrame extends BaseAnimationFrame {
    /**
     * 默认常量类型
     */
    public static final int TYPE = 2;
    private long lastClickTimeMillis;
    private int likeCount;

    public TextAnimationFrame(long duration) {
        super(duration);
    }


    @Override
    public void prepare(int x, int y, BitmapProvider.Provider bitmapProvider) {
        reset();
        setStartPoint(x, y);
        calculateCombo();
        elements = generatedElements(x, y, bitmapProvider);
    }

    private void calculateCombo() {
        if (System.currentTimeMillis() - lastClickTimeMillis < duration) {
            likeCount++;
        } else {
            likeCount = 1;
        }
        lastClickTimeMillis = System.currentTimeMillis();
    }

    @Override
    public int getType() {
        return TYPE;
    }


    @Override
    public boolean onlyOne() {
        return true;
    }

    /**
     * 生成N个Element
     *
     * @param x
     * @param y
     * @param bitmapProvider
     * @return Element集合
     */
    protected List<Element> generatedElements(int x, int y, BitmapProvider.Provider bitmapProvider) {
        List<Element> elements = new ArrayList<>();
        int count = likeCount;
        int offset_x = 0;
        while (count > 0) {
            int number = count % 10;
            PixelMap bitmap = bitmapProvider.getNumberBitmap(number);
            Size size = bitmap.getImageInfo().size;
            offset_x += size.width;
            Element element = new TextElement(bitmap, x - offset_x);
            elements.add(element);
            count = count / 10;
        }

        int level = likeCount / 10;
        if (level > 2) {
            level = 2;
        }
        elements.add(new TextElement(bitmapProvider.getLevelBitmap(level), x));
        return elements;
    }

    /**
     * 文字Element
     */
    public static class TextElement implements Element {
        private int x;
        private int y;
        private PixelMap bitmap;

        public TextElement(PixelMap bitmap, int x) {
            this.bitmap = bitmap;
            this.x = x;
        }

        @Override
        public int getX() {
            return x;
        }

        @Override
        public int getY() {
            return y;
        }

        @Override
        public PixelMap getBitmap() {
            return bitmap;
        }

        @Override
        public void evaluate(int startX, int startY, double time) {
            Size size = bitmap.getImageInfo().size;
            y = startY - 500 - (size.height / 2);
        }

        @Override
        public Paint getPaint() {
            return new Paint();
        }
    }
}
